package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: HWW
 * @Date: 2021/8/3015:32
 * @Desc:
 */
@Slf4j
@Controller
public class AlipayController {

    private String APP_ID = "2021000118609997";

    private String APP_PRIVATE_KEY ="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCiqr+BNK0cAmqqynEeS1EHYyI18AntbFS0seR5+CXIuxD3P6jlZNAo2zRiBQ21DUR6Neh0EqPRG7P6vI8Epn2kmo1kuPxm0TPOWZP7Mmw/THCmcZAuAvOuycq3wNkkHWi9TcF9W1vq4D+yP79w8RkQFKsgqJMYZaxq1IrTsC2gZ/xnJ4rc2lg7R4qxFY1h4qeuqN7+hNMCdISKI8sq5f0B+AiA5auipW4RR+rOBuwhVDWs7gXhc3ffnR5UaXqn7xrrn//RPd5ePVe0zdzwwLx2wMMATRlRDy6gH9uRbsJ/sEWszAy7k9zPlHxiKxwaDobu9kUIgyMNPxx/MiR1zHDvAgMBAAECggEBAJEMnt2Q4jgSs5j+EeZIlMV4kX31QHT034eNBUX5j3+xx0wh+PIsdi2oYcpx5Son3YL4ZQJzyJ3jDRctYn5sE6depWxGQ9NPAtojKICbEslS2TSiGUXeHfsjN1+pWDcK/mWnWzzz5T/InSPOEmCcrJOZSotBpoDf0OImCnD/LYBVJgmdhHrAq4skHJ1MH/99lCzDDdtbD51BZSaXN9sGf4nZaSma9SrokeoavU6GlOF5MtYNDzjGsd99gbN0iL7JZ1/uqnHIKkkNn4Pmx2zoJzUnOVQ5GgwoUP+5rA16WIKoUzZaSLhJZNdKKyW8NjhQUxAnqtn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ZLj8ZtEzzlmT+zJsP0xwpnGQLgLzrsnKt8DZJB1ovU3BfVtb6uA/sj+/cPEZEBSrIKiTGGWsatSK07AtoGf8ZyeK3NpYO0eKsRWNYeKnrqje/oTTAnSEiiPLKuX9AfgIgOWroqVuEUfqzgbsIVQ1rO4F4XN3350eVGl6p+8a65//0T3eXj1XtM3c8MC8dsDDAE0ZUQ8uoB/bkW7Cf7BFrMwMu5Pcz5R8YiscGg6G7vZFCIMjDT8cfzIkdcxw7wIDAQAB";

    private String alipayURL = "https://openapi.alipaydev.com/gateway.do";
    @GetMapping("/alipaytest")
    public void doPost(HttpServletRequest httpRequest,
                       HttpServletResponse httpResponse) throws ServletException, IOException {
        AlipayClient alipayClient = new DefaultAlipayClient(alipayURL, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        // alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        // alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101002\"," +
                " \"total_amount\":\"9999.99\"," +
                " \"subject\":\"Iphone12pro MAX 512G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
